/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author Reynusa
 */
public class DatabaseConnect {
     // Sesuaikan properti jdbcURL dengan basis data anda

    private String jdbcURL = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
    private String user = "mhs125314075";
    private String password = "mhs125314075";
//    private String jdbcURL = "jdbc:oracle:thin:@localhost:1521:xe";
//    private String user = "reynusa";
//    private String password = "reynusa";
    public Connection connection = null;

    /**
     * Creates a new instance of UserConnection
     */
    public DatabaseConnect() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(jdbcURL, user, password);
            System.out.println("koneksi berhasil");
        } catch (Exception ex) {
            System.out.println("message: " + ex.getMessage());
        }
    }
        public static Connection open(String url, String userId, String password) {
        Connection conn = null;
        try {
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(url);
            conn = ds.getConnection(userId, password);
            System.out.println("Koneksi is OK");
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
        return conn;
    }

    public boolean isConnected() {
        if (connection != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getClosed() {
        if (isConnected()) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return true;
        } else {
            return false;
        }
    }

    public String getJdbcURL() {
        return jdbcURL;
    }

    public void setJdbcURL(String jdbcURL) {
        this.jdbcURL = jdbcURL;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConn(Connection connection) {
        this.connection = connection;
    }
    
//    public String selecttagihan(String u, String pass){
//        return "select username, password, beli, sewa, jumlahbeli," +
//                "jumlah sewa from online where username = '" + u + "' AND password = '"
//                + pass + "'";
//    }
    
//    public String selecttagihan(){
//        return "select beli, jumlahbeli," +
//                 "'";
//    }

    public static void main(String[] args) throws SQLException {
        DatabaseConnect conn = new DatabaseConnect();
//        String query = "SELECT * FROM PENJUALAN";
//        java.sql.Statement statement = conn.getConnection().createStatement();
//        java.sql.ResultSet lihat = statement.executeQuery(query);
//        try {
//            while (lihat.next()) {
////                System.out.println(lihat.getString(1) + " - " + lihat.getString(3));
//                System.out.println("id      : " + lihat.getString(1));
//                System.out.println("jenis   : " + lihat.getString(2));
//                System.out.println("judul   : " + lihat.getString(3));
//                System.out.println("harga   : " + lihat.getString(4));
//                System.out.println("");
//            }
//        } catch (Exception ex) {
//            System.out.println("message: " + ex.getMessage());
        }
//    }
    
}


